﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace TInventory
{
    public partial class InvoiceListing : Form
    {
        TIEntities dbContext;
        public Form parentForm;
        
        public InvoiceListing()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonAddCustomer_Click(object sender, EventArgs e)
        {
            AddEditCustomer form = new AddEditCustomer();


            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void RefreshGrid()
        {
            try
            {
                DateTime tempDate = this.dateTimePickerStart.Value.Subtract(TimeSpan.FromDays(1));
                DateTime dtStart = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                tempDate = this.dateTimePickerLast.Value.AddDays(1);
                DateTime dtEnd = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                //var listOfCustomers = (from sup in dbContext.Customers select sup).ToList<Customer>();
                var listOfInvoices = (from st in dbContext.SalesTransactions where st.DATE >= dtStart && st.DATE <= dtEnd select new { st.DATE, st.Customer.CustomerCompanyName, st.INVOICE_ID }).Distinct().ToList();
                dataGridViewCustomer.DataSource = listOfInvoices;

                
                
                dataGridViewCustomer.Columns["DATE"].HeaderCell.Value = "Date";
                dataGridViewCustomer.Columns["CustomerCompanyName"].HeaderCell.Value = "Customer Company Name";                
                dataGridViewCustomer.Columns["INVOICE_ID"].HeaderCell.Value = "Invoice Number";

                this.dataGridViewCustomer.DataSource = listOfInvoices;
            }
            catch (Exception e1)
            {
                MessageBox.Show("Some Error Occured");

            }
        }

        private void InvoiceListing_Load(object sender, EventArgs e)
        {
            this.dateTimePickerLast.Value = DateTime.Now;
            this.dateTimePickerStart.Value = DateTime.Now.Subtract(TimeSpan.FromDays(7));  

            RefreshGrid();            
        }

        private void buttonEditSelected_Click(object sender, EventArgs e)
        {
            AddEditCustomer form = new AddEditCustomer();

            form.selectedRecord = dataGridViewCustomer.SelectedRows[0];
            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

       

       
        private void buttonDeleteSelected_ControlRemoved(object sender, ControlEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RefreshGrid();
        }

      
    }
}
